﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EmailTool
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
            this.txt_to.Text = "xxx@qq.com";
            this.txt_ccs.Text = "xxx@163.com;xxx@gmail.com;";
            this.txt_title.Text = "Mail Send Test ";
            this.txt_body.Text = "<br /><br /><div><div style=\"color:#909090;font-family:Arial Narrow;font-size:12px\">------------------</div><div style=\"font-size:14px;font-family:Verdana;color:#000;\"><div><p style=\" line-height: 17.85pt \"><span lang=\"ZH-CN\" style=\"font-size: 10.5pt; font-family: SimSun;\">REAA Tech</span></p><p style=\" line-height: 17.85pt \"><span lang=\"ZH-CN\" style=\"font-size: 10.5pt; font-family: SimSun;\">Shenzhen REAA Network Tech Co., LTD</span></p><p style=\" line-height: 17.85pt \"><span lang=\"ZH-CN\" style=\"font-size: 10.5pt;\">DevTeam&nbsp;</span></p><p style=\" line-height: 17.85pt \"><span lang=\"ZH-CN\" style=\"font-size: 10.5pt;\">Addr:xxx</span></p><p style=\" line-height: 17.85pt \"><span lang=\"ZH-CN\" style=\"font-size: 10.5pt;\">18/F,xxx,Futian Shenzhen</span></p><p style=\" line-height: 17.85pt \"><span lang=\"ZH-CN\" style=\"font-size: 10.5pt;\">Tel:0000</span></p></div></div></div>";
        }

        private void btn_send_Click(object sender, EventArgs e)
        {
            try
            {
                //邮件内容
                var emo = new EmailParaModel()
                {
                    EnableSsl = true,
                    Host = this.txt_host.Text,
                    Port = Convert.ToInt32(this.txt_port.Text),
                    UserName = this.txt_from.Text,
                    Password = this.txt_pwd.Text,
                    MailFrom = this.txt_from.Text,
                    MailTo = this.txt_to.Text,
                    MailCc = this.txt_ccs.Text,
                    Subject = this.txt_title.Text,
                    Body = this.txt_body.Text,
                    IsBodyHtml = true,
                    Priority = 0,
                };
                emo.AttachFiles = new Dictionary<string, string>();

                //添加附件
                var arrs = this.txt_files.Text.Split(',');
                if (arrs != null && arrs.Count() > 0)
                {
                    foreach (var item in arrs)
                    {
                        if (File.Exists(item))
                        {
                            var fname = Path.GetFileName(item);
                            emo.AttachFiles.Add(fname, item);
                        }
                    }
                }
                this.txt_error.Text = "sending...";
                //发信
                EmailHelper.SendNetMailAsync(emo, m_OnSendCompleted);
            }
            catch (AggregateException ex)
            {
                MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
            }
        }

        private void m_OnSendCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e?.Error != null)
            {
                this.txt_error.Text = e.Error.Message + e.Error?.InnerException?.Message;
            }
            else
            {
                this.txt_error.Text = "success";
            }
        }

        private void btn_addfile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Multiselect = true;
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                this.txt_files.Text = string.Join(",", openFile.FileNames);
            }
        }
    }
}
